﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Learn001.Controls
{
    [TemplatePart(Name = StateButton, Type = typeof(PanelButton))]
    [TemplatePart(Name = ContentContainer, Type = typeof(ContentControl))]
    [TemplatePart(Name = PanelTitle, Type = typeof(TextBlock))]
    [TemplatePart(Name = PanelFooter, Type = typeof(TextBlock))]
    public abstract class CustomPanel : ContentControl
    {
        protected internal const string StateButton = "btnMax";
        protected internal const string ContentContainer = "contentControl";
        protected internal const string PanelTitle = "txtTitle";
        protected internal const string PanelFooter = "txtFooter";

        protected internal PanelButton btnMax;
        public PanelButton Button
        {
            get { return btnMax; }
        }
        protected internal ContentControl contentControl;
        protected internal TextBlock txtTitle;
        protected internal TextBlock txtFooter;

        public event RoutedEventHandler Click;

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(CustomPanel), new PropertyMetadata("", new PropertyChangedCallback(TitleChanged)));

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        private static void TitleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            CustomPanel panel = sender as CustomPanel;
            if (panel == null) return;

            if (e.NewValue != null && panel.txtTitle != null)
                panel.txtTitle.Text = e.NewValue.ToString();
            else if (panel.txtTitle != null)
                panel.txtTitle.Text = "";
        }

        public static readonly DependencyProperty FooterProperty =
            DependencyProperty.Register("Footer", typeof(string), typeof(CustomPanel), new PropertyMetadata("", new PropertyChangedCallback(FooterChanged)));

        public string Footer
        {
            get { return (string)GetValue(FooterProperty); }
            set { SetValue(FooterProperty, value); }
        }

        private static void FooterChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            CustomPanel panel = sender as CustomPanel;
            if (panel == null) return;

            if (e.NewValue != null && panel.txtFooter != null)
            {
                panel.txtFooter.Text = e.NewValue.ToString();
                panel.txtFooter.Visibility = Visibility.Visible;
            }
            else if (panel.txtFooter != null)
            {
                panel.txtFooter.Text = "";
                panel.txtFooter.Visibility = Visibility.Collapsed;
            }
        }

        public new static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(UIElement), typeof(CustomPanel), new PropertyMetadata(null, new PropertyChangedCallback(ContentChanged)));

        public new UIElement Content
        {
            get { return (UIElement)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        private static void ContentChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            CustomPanel panel = sender as CustomPanel;
            if (panel == null) return;

            if (e.NewValue != null && panel.contentControl != null)
                panel.contentControl.Content = e.NewValue;
            else if (panel.contentControl != null)
                panel.contentControl.Content = null;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            btnMax = base.GetTemplateChild(StateButton) as PanelButton;
            contentControl = base.GetTemplateChild(ContentContainer) as ContentControl;
            txtTitle = base.GetTemplateChild(PanelTitle) as TextBlock;
            txtFooter = base.GetTemplateChild(PanelFooter) as TextBlock;

            if (btnMax != null)
                btnMax.Click += new RoutedEventHandler(btnMax_Click);

            if (contentControl != null)
                contentControl.Content = Content;

            if (txtTitle != null)
                txtTitle.Text = Title;

            if (txtFooter != null)
            {
                txtFooter.Text = Footer;
                if (Footer.Trim() != "")
                    txtFooter.Visibility = System.Windows.Visibility.Visible;
                else
                    txtFooter.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void btnMax_Click(object sender, RoutedEventArgs e)
        {
            if (Click != null)
                Click(this, e);
        }
    }
}
